package com.wngbms;

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.producer.*;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * max.block.ms = 60000
 * metadata.max.age.ms = 300000
 * metadata.max.idle.ms = 300000
 * reconnect.backoff.max.ms = 1000
 * reconnect.backoff.ms = 50
 * request.timeout.ms = 30000
 * retry.backoff.ms = 100
 *
 * 	acks = -1
 * 	batch.size = 16384
 * 	bootstrap.servers = [localhost:9092]
 * 	buffer.memory = 33554432
 * 	client.dns.lookup = use_all_dns_ips
 * 	client.id = producer-1
 * 	compression.type = none
 * 	connections.max.idle.ms = 540000
 * 	delivery.timeout.ms = 120000
 * 	enable.idempotence = true
 * 	interceptor.classes = []
 * 	key.serializer = class org.apache.kafka.common.serialization.StringSerializer
 * 	linger.ms = 0
 * 	max.block.ms = 60000
 * 	max.in.flight.requests.per.connection = 5
 * 	max.request.size = 1048576
 * 	metadata.max.age.ms = 300000
 * 	metadata.max.idle.ms = 300000
 * 	metric.reporters = []
 * 	metrics.num.samples = 2
 * 	metrics.recording.level = INFO
 * 	metrics.sample.window.ms = 30000
 * 	partitioner.class = class org.apache.kafka.clients.producer.internals.DefaultPartitioner
 * 	receive.buffer.bytes = 32768
 * 	reconnect.backoff.max.ms = 1000
 * 	reconnect.backoff.ms = 50
 * 	request.timeout.ms = 30000
 * 	retries = 2147483647
 * 	retry.backoff.ms = 100
 * 	sasl.client.callback.handler.class = null
 * 	sasl.jaas.config = null
 * 	sasl.kerberos.kinit.cmd = /usr/bin/kinit
 * 	sasl.kerberos.min.time.before.relogin = 60000
 * 	sasl.kerberos.service.name = null
 * 	sasl.kerberos.ticket.renew.jitter = 0.05
 * 	sasl.kerberos.ticket.renew.window.factor = 0.8
 * 	sasl.login.callback.handler.class = null
 * 	sasl.login.class = null
 * 	sasl.login.refresh.buffer.seconds = 300
 * 	sasl.login.refresh.min.period.seconds = 60
 * 	sasl.login.refresh.window.factor = 0.8
 * 	sasl.login.refresh.window.jitter = 0.05
 * 	sasl.mechanism = GSSAPI
 * 	security.protocol = PLAINTEXT
 * 	security.providers = null
 * 	send.buffer.bytes = 131072
 * 	socket.connection.setup.timeout.max.ms = 30000
 * 	socket.connection.setup.timeout.ms = 10000
 * 	ssl.cipher.suites = null
 * 	ssl.enabled.protocols = [TLSv1.2]
 * 	ssl.endpoint.identification.algorithm = https
 * 	ssl.engine.factory.class = null
 * 	ssl.key.password = null
 * 	ssl.keymanager.algorithm = SunX509
 * 	ssl.keystore.certificate.chain = null
 * 	ssl.keystore.key = null
 * 	ssl.keystore.location = null
 * 	ssl.keystore.password = null
 * 	ssl.keystore.type = JKS
 * 	ssl.protocol = TLSv1.2
 * 	ssl.provider = null
 * 	ssl.secure.random.implementation = null
 * 	ssl.trustmanager.algorithm = PKIX
 * 	ssl.truststore.certificates = null
 * 	ssl.truststore.location = null
 * 	ssl.truststore.password = null
 * 	ssl.truststore.type = JKS
 * 	transaction.timeout.ms = 60000
 * 	transactional.id = null
 * 	value.serializer = class org.apache.kafka.common.serialization.StringSerializer
 */
@Slf4j
public class CustomProducer {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 0 配置
        Properties properties = new Properties();

        // 连接集群 bootstrap.servers
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"localhost:9092");

        // 指定对应的key和value的序列化类型 key.serializer
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.class.getName());

        /**
         *  * max.block.ms = 60000
         *  * metadata.max.age.ms = 300000
         *  * metadata.max.idle.ms = 300000
         *  * reconnect.backoff.max.ms = 1000
         *  * reconnect.backoff.ms = 50
         *  * request.timeout.ms = 30000
         *  * retry.backoff.ms = 100
         */
        properties.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, 60000 * 100);
        properties.put(ProducerConfig.METADATA_MAX_AGE_CONFIG, 300000 * 100);
        properties.put(ProducerConfig.METADATA_MAX_IDLE_CONFIG, 300000 * 100);
        properties.put(ProducerConfig.RECONNECT_BACKOFF_MAX_MS_CONFIG, 1000 * 100);
        properties.put(ProducerConfig.RECONNECT_BACKOFF_MS_CONFIG, 50 * 100);
        properties.put(ProducerConfig.REQUEST_TIMEOUT_MS_CONFIG, 30000 * 100);
        properties.put(ProducerConfig.RETRY_BACKOFF_MS_CONFIG, 100 * 100);

        // 1 创建kafka生产者对象
        // "" hello
        KafkaProducer<String, String> kafkaProducer = new KafkaProducer<>(properties);

        // 2 发送数据
        for (int i = 0; i < 3; i++) {
            Future<RecordMetadata> future = kafkaProducer.send(new ProducerRecord<>("first", "atguigu" + i), new Callback() {
                @Override
                public void onCompletion(RecordMetadata metadata, Exception exception) {
                    if (exception == null){
                        log.info("on_completion:{}", metadata);
                    }
                }
            });
            RecordMetadata metadata = future.get();
            log.info("metadata:{}", metadata);
        }

        // 3 关闭资源
        kafkaProducer.close();
    }
}
